/**
 * Copyright (C), 2015-2018, 成都启迪信息技术有限公司
 * FileName: CacheService
 * Author:   sjkyll
 * Date:     2018-08-06 16:35
 * Description: 缓存修改
 * <author>          <time>          <version>          <desc>
 * 作者姓名         修改时间           版本号            描述
 */
package com.cdqidi.smsservice.service;

import com.cdqidi.smsservice.domain.CpConfig;
import com.cdqidi.smsservice.repository.CpConfigRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Optional;

/**
 * 〈一句话功能简述〉<br> 
 *
 * @author sjkyll
 * @create 2018-08-06 16:35
 * @since 1.0.0
 */
@Service
public class CacheService {
    @Autowired
    private CpConfigRepository cpConfigRepository;
    @Autowired
    private CpConfigService cpConfigService;

    @Cacheable(value = "apiCache", key = "#appId")
    public Optional<CpConfig> getCpConfig(String appId) {
        return cpConfigRepository.findById(appId);
    }

    @CacheEvict(allEntries = true, value = "apiCache")
    public void flushCpConfig() {
        cpConfigService.flushMap();
    }

    @CacheEvict(value = "apiCache", key = "#appId")
    public Optional<CpConfig> updateCpConfig(String appId){
        return cpConfigRepository.findById(appId);
    }

    @Transactional
    @CacheEvict(value = "apiCache", key = "#appId")
    public void deleteCpConfig(String appId){
        cpConfigRepository.deleteById(appId);
        cpConfigService.deleteOneCache(appId);
    }
}